<template>
    <div>
        <a-alert
             :message="i18n('button.buttonShow', '满足以下条件，可以显示按钮')"
             :description="i18n('button.enterJs', '在下面代码编辑器输入JAVA代码作为显示条件，需要返回布尔值，可以点击字段快捷插入')"
             type="info"
             show-icon
        />
        <div style="height: 200px;overflow: hidden;padding-top: 10px;">
            <rx-monaco-editor
                ref="javaCode"
                v-model="source.condition"
                language="java"
            >
                <template v-slot:monacoeditor>
                    <a-dropdown :overlayClassName="'script-a-dropdown'">
                        <a-menu slot="overlay">
                            <a-sub-menu :key="table.name" :title="table.title"  v-for="table in tables">
                                <a-menu-item v-for="field in table.children" @click="insertField(table,field)">{{displayField(field)}} </a-menu-item>
                            </a-sub-menu>
                        </a-menu>
                        <a-button style="margin-left: 8px"> {{ i18n('button.field', '选择字段') }} <a-icon type="down" /> </a-button>
                    </a-dropdown>

                    <a-dropdown :overlayClassName="'script-a-dropdown'">
                        <a-menu slot="overlay">
                            <a-sub-menu :key="item.name" :title="item.name" v-for="item in contextAndOperator">
                                <a-menu-item v-for="row in item.items" @click="insertScript(row.value)">{{row.name}}</a-menu-item>
                            </a-sub-menu>
                        </a-menu>
                        <a-button style="margin-left: 8px"> {{ i18n('button.context', '上下文及运算') }} <a-icon type="down" /> </a-button>
                    </a-dropdown>

                    <a-button style="margin-left: 8px" @click="showScriptDialog()"  >
                        {{ i18n('button.fastScript', '快速脚本') }}
                    </a-button>

                </template>
            </rx-monaco-editor>
        </div>
    </div>
</template>
<script >

import { RxMonacoEditor,Dialog,Util } from "@lowcode/jpaas-common-lib";
import contextAndOperator   from "@/views/modules/form/core/TableForrmula/contextAndOperator";
import FormBoDefApi from "@/api/form/bo/formBoDef";
export default {
    props:{

        source: {
            type: Object
        }
    },
    components:{
        RxMonacoEditor
    },
    data(){
        return {

            contextAndOperator: contextAndOperator,
            tables:[]
        }
    },
    created(){
        this.initFields();
    },
    methods:{
        i18n(name, text, key) {
            return this.$ti18(name, text, "formTableFormula", key);
        },
        initFields(){
            let boDefId=this.source.boDefId;
            let type=this.source.type;
            let boName=this.source.boName;
            let self_=this;

            FormBoDefApi.getBoDefConstruct(boDefId,false,'',true).then((res) => {
                let mainFields=res.data.filter(item=>{return item.type==="main"});
                let subTables=res.data.filter(item=>{return item.type!="main"});
                self_.tables.push({name:"main",title:"主表",type:"main",children:[...mainFields]});

                for(let i=0;i<subTables.length;i++){
                    let table=subTables[i];
                    if(table.type=="onetoone" || table.name==boName){
                        self_.tables.push({name:table.name,title:table.title,type:table.type, children:table.children});
                    }
                }
            })

        },
        showScriptDialog() {
            let self = this;
            let config = {curVm: this};
            Dialog.openScriptDialog(config, function (script) {
                self.insertScript(script);
            })

        },
        insertScript(script) {
            this.$refs.javaCode.insertContent(script);
        },
        displayField(field){
            if(field.control!='address' && field.isSingle==0){
                return field.title + "(双值)" ;
            }
            return  field.title ;
        },
        insertField(table,field) {
            let boName=this.source.boName;
            let str="";
            if(table.name == boName || (this.source.isMain == "yes" && table.name=="main" )){
                str="cur." + field.name ;
            }
            else{
                if(table.type=="onetoone"){
                    str="sub."+table.name+"."+field.name ;
                }
                else if(table.type=="main"){
                    str="mainCur."+field.name ;
                }
            }
            this.insertScript(str);
        },
    },

}
</script>



<style>
.script-a-dropdown .ant-dropdown-menu-submenu .ant-dropdown-menu{
    background: #fff;
    max-height: 300px;
    overflow-y: auto;
}
</style>